package com.tlgen.orm.controller;

import com.tlgen.orm.common.R;
import com.tlgen.orm.domain.Book;
import com.tlgen.orm.common.Model;
import com.tlgen.orm.factory.QueryOperator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @GetMapping("/select")
    public List<Book> select() {
        return Model.selectAll(Book.class);
    }

    @GetMapping("/selectList")
    public List<Book> selectList() {
        return Model.selectAll(new QueryOperator<Book>()
                .like(Book::getAuthor, "小"), Book.class);
    }

    @GetMapping("/selectList2")
    public List<Book> selectList2() {
        return Model.selectAll(new QueryOperator<Book>()
                .like(Book::getName, "小"), Book.class);
    }

    @GetMapping("/selectOne")
    public Book selectOne() {
        return Model.selectOne(new QueryOperator<Book>()
                .like(Book::getAuthor, "小"), Book.class);
    }

    @GetMapping("/selectById")
    public Book selectById() {
        return Model.selectById("2", Book.class);
    }

    @GetMapping("/save")
    public R save() {
        Book book = new Book();
        book.setName("权力游戏");
        book.setAuthor("晓晓");
        Model.save(book);
        return R.ok();
    }

    @GetMapping("/saveBatch")
    public Object saveBatch() {
        List<Book> bookList = new ArrayList<>();
        Book book1 = new Book();
        book1.setName("怪剑");
        book1.setAuthor("影月");
        book1.setPrice(new BigDecimal(26));
        bookList.add(book1);
        Book book2 = new Book();
        book2.setName("简单化");
        book2.setAuthor("明明");
        bookList.add(book2);
        Model.saveBatch(bookList);
        return true;
    }

    @GetMapping("/update")
    public Object update() {
        Book book = new Book();
        book.setId("12");
        book.setName("权力游戏");
        book.setAuthor("晓晓");
        Model.update(book);
        return true;
    }

    @GetMapping("/updateSelective")
    public Object updateSelective() {
        Book book = new Book();
        book.setId("12");
        book.setName("权力游戏");
        book.setAuthor("晓晓");
        Model.updateSelective(book);
        return true;
    }

    @GetMapping("/updateSelective2")
    public Object updateSelective2() {
        Book book = new Book();
        book.setId("12");
        book.setName("权力游戏");
        book.setAuthor("晓晓");
        Model.updateSelective(new QueryOperator<Book>().eq(Book::getName, "权利游戏"), book);
        return true;
    }

    @GetMapping("/updateList")
    public Object updateList() {
        Book book = new Book();
        book.setName("怪剑奇谭");
        Model.update(new QueryOperator<Book>().eq(Book::getName, "怪剑1"), book);
        return true;
    }

    @GetMapping("/update2")
    public Object update2() {
        Book book = Model.selectById("1", Book.class);
        book.setName("南光极东");
        Model.update(book);
        return true;
    }

    @GetMapping("/deleteById")
    public Object deleteById() {
        Model.deleteById("5", Book.class);
        return true;
    }

    @GetMapping("/deleteList")
    public Object deleteList() {
        Model.delete(new QueryOperator<Book>()
                .eq(Book::getName, "简单化")
                .eq(Book::getAuthor, "明明"), Book.class);
        return true;
    }

    @GetMapping("/delete")
    public Object delete() {
        Model.delete(Book.class);
        return true;
    }

    @GetMapping("/like")
    public Object like() {
        return Model.selectAll(new QueryOperator<Book>().like(Book::getName, "力"), Book.class);
    }

    @GetMapping("/notLike")
    public Object notLike() {
        return Model.selectAll(new QueryOperator<Book>().notLike(Book::getName, "怪"), Book.class);
    }

    @GetMapping("/likeLeft")
    public Object likeLeft() {
        return Model.selectAll(new QueryOperator<Book>().likeLeft(Book::getName, "东"), Book.class);
    }

    @GetMapping("/likeRight")
    public Object likeRight() {
        return Model.selectAll(new QueryOperator<Book>().likeRight(Book::getName, "权"), Book.class);
    }

    @GetMapping("/ne")
    public Object ne() {
        return Model.selectAll(new QueryOperator<Book>().ne(Book::getName, "南光极东"), Book.class);
    }

    @GetMapping("/or")
    public Object or() {
        return Model.selectAll(new QueryOperator<Book>().eq(Book::getName, "南光极东").or(Book::getName, "教父"), Book.class);
    }

    @GetMapping("/in")
    public Object in() {
        return Model.selectAll(new QueryOperator<Book>().in(Book::getAuthor, Arrays.asList("晓晓", "c论")), Book.class);
    }

    @GetMapping("/notIn")
    public Object notIn() {
        return Model.selectAll(new QueryOperator<Book>().notIn(Book::getAuthor, Arrays.asList("晓晓", "c论")), Book.class);
    }

    @GetMapping("/lt")
    public Object lt() {
        return Model.selectAll(new QueryOperator<Book>().lt(Book::getPrice, 30), Book.class);
    }

    @GetMapping("/le")
    public Object le() {
        return Model.selectAll(new QueryOperator<Book>().le(Book::getPrice, 30), Book.class);
    }

    @GetMapping("/gt")
    public Object gt() {
        return Model.selectAll(new QueryOperator<Book>().gt(Book::getPrice, 20), Book.class);
    }

    @GetMapping("/ge")
    public Object ge() {
        return Model.selectAll(new QueryOperator<Book>().ge(Book::getPrice, 20), Book.class);
    }

    @GetMapping("/between")
    public Object between() {
        return Model.selectAll(new QueryOperator<Book>().between(Book::getPrice, 20, 30), Book.class);
    }

    @GetMapping("/notBetween")
    public Object notBetween() {
        return Model.selectAll(new QueryOperator<Book>().notBetween(Book::getPrice, 20, 30), Book.class);
    }

    @GetMapping("/isNull")
    public Object isNull() {
        return Model.selectAll(new QueryOperator<Book>().isNull(Book::getPrice), Book.class);
    }

    @GetMapping("/isNotNull")
    public Object isNotNull() {
        return Model.selectAll(new QueryOperator<Book>().isNotNull(Book::getPrice), Book.class);
    }

    @GetMapping("/groupBy")
    public Object groupBy() {
        return Model.selectAll(new QueryOperator<Book>().groupBy(Book::getId, Book::getName, Book::getPrice), Book.class);
    }

    @GetMapping("/groupBy2")
    public Object groupBy2() {
        return Model.selectAll(new QueryOperator<Book>().groupBy("id", "name", "price"), Book.class);
    }

    @GetMapping("/orderByAsc")
    public Object orderByAsc() {
        return Model.selectAll(new QueryOperator<Book>().orderByAsc(Book::getId, Book::getPrice), Book.class);
    }

    @GetMapping("/orderByAsc2")
    public Object orderByAsc2() {
        return Model.selectAll(new QueryOperator<Book>().orderByAsc("id", "price"), Book.class);
    }

    @GetMapping("/orderByDesc")
    public Object orderByDesc() {
        return Model.selectAll(new QueryOperator<Book>().orderByDesc(Book::getPrice), Book.class);
    }

    @GetMapping("/orderByDesc2")
    public Object orderByDesc2() {
        return Model.selectAll(new QueryOperator<Book>().orderByDesc("price"), Book.class);
    }

}

